<?php

namespace app\load\service;

use app\load\loadModel\AdminModel;
use app\load\loadModel\NoteModel;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;

/**
 * 公告服务表
 */
class NotesService
{
    /**
     * 列表
     * @param array $where
     * @param string $filed
     * @return \app\load\model\note\NoteModel[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function list(array $where = [] , string $filed = '*'){

        return NoteModel::Note()->where($where)
            ->field($filed)
            ->withAttr('admin_name', function ($v){
                return AdminModel::Admin()->where('id' , $v)->value('name');
            })
            ->select();
    }

    /**
     * 详情
     * @param array $where
     * @param string $field
     * @return \app\load\model\note\NoteModel[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function info(array $where , string $field = '*')
    {
        return NoteModel::Note()->where($where)
            ->field($field)
            ->filter(function ($v){
                $v['admin_name'] = AdminModel::Admin()->where('id' , $v['admin_id'])->value('name');
                $v['cate_name'] = NoteModel::NoteCate()->where('id' , $v['cate_id'])->value('title');
            })
            ->find();
    }

    /**
     * 类型列表
     * @param array $where
     * @param string $filed
     * @return \app\load\model\note\NoteModel[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function listType(array $where = [] , string $filed = '*')
    {
        return NoteModel::NoteCate()->where($where)
            ->field($filed)
            ->order('sort desc')
            ->select();
    }


}